﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;
using System.Text.RegularExpressions;
using Portal.CMS;
using System.Globalization;

namespace Portal.CDSP
{
    class Ultility
    {

        public static string GetAlbumDetails(string belongSite, string Subject, string ShortId, string AlbumType)
        {
            Subject = Environments.ConvertSubject(Subject);
            string Url = string.Empty;
            if (AlbumType.Equals("book"))
            {
                Url = Environments.FrontHttpRoot + "/" + belongSite + "/" + Subject + "-bk-" + ShortId + "." + Environments.Extension;//?NewsId=" + ShortId;                          
            }
            else
            {
                Url = Environments.FrontHttpRoot + "/" + belongSite + "/" + Subject + "-va-" + ShortId + "." + Environments.Extension;//?NewsId=" + ShortId;           
            }
            if (Environments.MultiLanguageInSite == 1)
            {
                Url = Url + "?lang=" + Environments.ActiveLanguage;
            }
            return Url;
        }

        public static string ClearHTMLTags(string Source)
        {
            if (string.IsNullOrEmpty(Source) || Source.Length == 0)
                return Source;
            string temp = Source;
            while (temp.IndexOf("<") != -1 && temp.IndexOf(">") != -1)
            {
                int start = temp.IndexOf("<");
                int end = temp.IndexOf(">");
                temp = temp.Remove(start, end - start + 1);
            }
            return temp;
        }


        public static string SplitContent(string ShortContent)
        {           
            const int MaxValue = 40;
            int Count = 0;
            int j = 0;
            ShortContent = Regex.Replace(ShortContent, @"<(.|\n)*?>", string.Empty);
            int Length = ShortContent.Length;
            for (int i = 1; i < ShortContent.Length; i++)
            {
                if (ShortContent[i - 1].ToString().Equals(" ") && !ShortContent[i].ToString().Equals(" "))
                    Count++;
                if (Count <= MaxValue)
                {
                    j = i;
                }
            }
            if (Count > MaxValue)
            {
                ShortContent = ShortContent.ToString().Remove(j, (Length - j));
                ShortContent = string.Concat(ShortContent, "...");
            }
            return ShortContent;
        }


        public static string ReturnDateTimeVN()
        {
            CultureInfo cul = new CultureInfo("vi-VN", true);
            DateTime temp = DateTime.Now.Date;
            string date = temp.ToString("dd/MM/yyyy");
            string SDate = "";
            switch (DateTime.Now.DayOfWeek)
            {
                case DayOfWeek.Monday:
                    SDate = "Thứ hai";
                    break;
                case DayOfWeek.Tuesday:
                    SDate = "Thứ ba";
                    break;
                case DayOfWeek.Wednesday:
                    SDate = "Thứ tư";
                    break;
                case DayOfWeek.Thursday:
                    SDate = "Thứ năm";
                    break;
                case DayOfWeek.Friday:
                    SDate = "Thứ sáu";
                    break;
                case DayOfWeek.Saturday:
                    SDate = "Thứ bảy";
                    break;
                case DayOfWeek.Sunday:
                    SDate = "Chủ nhật";
                    break;
            }
            string Html = "<div class=datetime>" +
                            "<b>" + SDate + ", Ngày " + date + " ," + DateTime.Now.ToShortTimeString() + " GMT+7</b>" +
                            "</div>";
            return Html;
        }


        public static string GetEmObjectVideo(string belongSite, string Alias)
        {
            AdvImageItemCls oAdvImageItemCls = new AdvImageBLL().CreateModelItemByAlias(Environments.ConnectionString, Environments.ActiveLanguage, Alias);
            if (oAdvImageItemCls == null)
            {
                return "";
            }
            string ObjectUrl = Environments.HttpMediaRoot + "/Media/" + Environments.PortalAlias + "/AdvImage/Images/" + belongSite + "/" + oAdvImageItemCls.AdvImageId + "/" + oAdvImageItemCls.FileImage;

            string RetHtml =
                "<div class=\"cssVideo_details\">" +
                "       <a href=\"" + ObjectUrl + "\"  " +
                "           style=\"color:#295c72; display:block;width:450px;height:338px\"  " +
                "           id=\"player" + oAdvImageItemCls.AdvImageId + "\"> " +
                "       </a> 	" +
                //"<div style=\"text-align:center;color:white;\">" + advImageName + "</div>" +
                "<!-- this will install flowplayer inside previous A- tag. -->" +
                "<script>" +
                "    flowplayer(\"player" + oAdvImageItemCls.AdvImageId + "\", \"flowplayer/flowplayer-3.1.2.swf\", \r\n" +
                "    { \r\n" +
                "        clip: { \r\n" +
                "			 " +
                "	        // these two configuration variables does the trick \r\n" +
                "	        autoPlay: false,  \r\n" +
                "	        autoBuffering: true // <- do not place a comma here   \r\n" +
                "       } \r\n" +
                "    });\r\n" +
                "</script>" +
                "</div>";

            return RetHtml;
        }


        public static string GetEmObjectAudio(string belongSite, string Alias)
        {
            AdvImageItemCls oAdvImageItemCls = new AdvImageBLL().CreateModelItemByAlias(Environments.ConnectionString, Environments.ActiveLanguage, Alias);
            if (oAdvImageItemCls == null)
            {
                return "";
            }
            string ObjectUrl = Environments.HttpMediaRoot + "/Media/" + Environments.PortalAlias + "/AdvImage/Images/" + belongSite + "/" + oAdvImageItemCls.AdvImageId + "/" + oAdvImageItemCls.FileImage;
            string RetHtml =
                        "<div class=\"cssAudio_details\">" +
                        "<p id=\"audioplayer_1\">" +
                        "Trình duyệt của bạn không hỗ trợ flash" +
                        "</p>  " +
                        "<script type=\"text/javascript\">" +
                        "AudioPlayer.embed(\"audioplayer_1\",{soundFile:\"" + ObjectUrl + "\",titles:\"" + oAdvImageItemCls.AdvImageItemName + "\"}); " +
                        "</script>  " +
                        "</div>";
            return RetHtml;
        }


        public static string ProcessEmbObject(string belongSite, string Content, string EmbVideo, string EmbAudio)
        {
            StringBuilder Html = new StringBuilder();
            Html.Append(Content);
            string[] VideoItems = EmbVideo.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);
            string[] AudioItems = EmbAudio.Split(new char[] { ';' }, StringSplitOptions.RemoveEmptyEntries);

            for (int iIndex = 0; iIndex < VideoItems.Length; iIndex++)
            {
                string rData = GetEmObjectVideo(belongSite, VideoItems[iIndex]);
                Html.Append(rData);
            }

            for (int iIndex = 0; iIndex < AudioItems.Length; iIndex++)
            {
                string rData = GetEmObjectAudio(belongSite, AudioItems[iIndex]);
                Html.Append(rData);
            }
            return Html.ToString();
        }
    }
}
